

from db import ObjLink
import func

class Stocks:
    stocks = []
    pages = 0
    stocksGroups = []

    @classmethod
    def init(cls):
        cls.redis = ObjLink.getRedisDB()
        cls.mongodb = ObjLink.getMongoDB()
        cls.collections = Stocks.mongodb.ceshi
        cls.getAllStocks()

    #从mongo获取股票
    @classmethod
    def getAllStocks(cls):
        if cls.stocks != []:
            return cls.stocks
        else:
            results = cls.collections.allstockname.find({}).sort("_id", -1).limit(1)
            for result in results:
                cls.stocks = result['data']
            return cls.stocks

    #code 000001.sz
    @classmethod
    def getDaily(cls, code):
        doc = Stocks.collections.daily.find_one({'code':code})
        if doc != None:
            return doc['data']
        else:
            return False


    #切分任务数据
    @classmethod
    def getStocksGroup(cls):
        i=0
        group = []
        for st in cls.stocks:
            i=i+1
            group.append(st)
            if i % cls.pages == 0 or i == len(cls.stocks)-1:
                cls.stocksGroups.append(group)
                group = []

    def __init__(self):
        pass


Stocks.init()